package com.example.wly.chapter4homework;

import android.Manifest;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    int page=0;
    int wordsPerPage=200;
    int raw=30;
    int col=50;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText tvText=(EditText)findViewById(R.id.tv_text);
        Button btnRead=(Button)findViewById(R.id.btn_read);
        btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                page++;
                String path= Environment.getExternalStorageDirectory().getPath();
                File file=new File(path);
                try {
                    FileInputStream in=new FileInputStream(file);
                    BufferedReader br=new BufferedReader(new InputStreamReader(in));
                    br.skip((page-1)*wordsPerPage);
                    String line="";
                    for(int i=0;i<wordsPerPage;i++){
                        int c=br.read();
                        line=line+(char)c;
                    }
                    tvText.setText(line);
                    br.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
     String[]perms={Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if(!EasyPermissions.hasPermissions(this.perms)){
            EasyPermissions.requestPermissions(this,"write sd",1,perms);
        }

    }
}
